fix: prevent data loss on memory update when embedding API is unreachable#116
Merged
Merged
Conversation
When embeddingApiUrl and embeddingApiKey are configured, the plugin previously marked itself as ready without verifying the endpoint was reachable. This caused silent failures on subsequent embed() calls.
Add a lightweight probe request during warmup that sends a minimal embedding ('ping') to validate the API is actually responding. If the probe fails, isWarmedUp stays false and isReady() correctly reports the system as not ready.
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
The handleUpdateMemory function had a critical bug: it deleted the old memory record before generating new embeddings. If the embedding API was unreachable (e.g., LM Studio not running), the exception thrown by embedWithTimeout would prevent the new record from being inserted, causing permanent data loss. Fix: generate new embeddings FIRST, then atomically delete the old record and insert the updated one within a SQLite transaction. Additionally: - Wrap handleAddMemory and client.ts addMemory SQLite operations in transactions for consistency - Add toBlob helper to both files - Vector backend updates occur after SQLite transaction completes Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the embedding API (e.g., LM Studio) is unreachable, modifying a memory via
handleUpdateMemorycauses permanent data loss.Root Cause
In
handleUpdateMemory(), the old memory record is deleted first (deleteVector) before generating new embeddings (embedWithTimeout). If the embedding call throws (API down), the exception propagates and the delete is never followed by an insert — the memory is gone forever.Fix
Reorder operations (
api-handlers.ts): Generate new embeddings FIRST. Only after successful embedding does the function proceed to delete-and-reinsert within an atomic SQLite transaction.API health probe (
embedding.ts): During warmup, whenembeddingApiUrlis configured, send a minimal probe request ("ping") to verify the endpoint is actually reachable before markingisWarmedUp = true. Previously, the plugin blindly trusted the configuration without validating connectivity.Transactional safety (
api-handlers.ts,client.ts): Wrap all memory write operations indb.transaction()for atomic consistency between SQLite and vector index operations.Changes
embedding.tsapi-handlers.ts,client.tsTesting
bun run tscpasses with zero errorsnpm linkverified locally — fix confirmed active in runtime